Repository navigation
Conversation
With -sOFFSCREEN_FRAMEBUFFER, -pthread and -sGL_SUPPORT_EXPLICIT_SWAP_CONTROL, a WebGL context that is proxied to the main thread and created with explicitSwapControl=false is supposed to be presented by the runtime at each main loop iteration. Three earlier changes each broke a part of this: - emscripten-core#22621 moved the swap out of the main loop runner into a hook on _emscripten_proxied_gl_context_activated_from_main_browser_thread, but attached it with a `__postjs` suffix. That is not a JS library decorator, so the hook was never emitted. The key was instead treated as a library symbol of its own, which showed up in builds that include the whole JS library (-sINCLUDE_FULL_LIBRARY, -sMAIN_MODULE=1). Use `__postset`. - emscripten-core#22621 left the dependency on the native emscripten_webgl_commit_frame() behind on $setMainLoop, and emscripten-core#23855 then removed it from there as unused. Since then nothing has exported that function to JS for the hook to call. Make the hook depend on it. Also drop the hook's `$registerPreMainLoop` dependency: since emscripten-core#27995 it is detected automatically, and listing it is reported as redundant. - emscripten-core#22157 narrowed the write of the explicitSwapControl flag in the context handle from i32 to i8 along with the EM_BOOL struct fields. The handle field is not an EM_BOOL: the hook reads it as an i32, and the WebGLContextHandle struct in webgl1.c declares it as uint32_t. The handle is malloc()ed, so the upper bytes were left uninitialized. Write it as an i32 again. No test passed -sGL_SUPPORT_EXPLICIT_SWAP_CONTROL, and the one test of a proxied implicit-swap context (test_emscripten_animate_canvas_element_size) only checks the exit code. Add a browser test that reads back the canvas to check that the frame was presented. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
_emscripten_proxied_gl_context_activated_from_main_browser_thread had no __sig, so with -sMEMORY64 the context handle was stored in GL.currentContext as a BigInt. The implicit swap hook, which is now emitted, then failed with 'Cannot mix BigInt and other types' when reading the explicitSwapControl field through it. Add a 'vp' signature so the handle is converted to a Number. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| #endif | ||
| #if GL_SUPPORT_EXPLICIT_SWAP_CONTROL | ||
| {{{ makeSetValue('handle', 0, 'webGLContextAttributes.explicitSwapControl', 'i8')}}}; | ||
| {{{ makeSetValue('handle', 0, 'webGLContextAttributes.explicitSwapControl', 'i32')}}}; |
There was a problem hiding this comment.
grep shows other places still using i8 for this?
Hmm, but So it is a bool there? |
The story with bool was that EM_BOOL used to be i8, but https://github.com/emscripten-core/emscripten/pull/22157/changes#diff-874f33563125619a7a5cb567ebe523c59258662d69858d78d24d947b275f9c6cR23-R24 changed it to a i32 (which was a breaking ABI change to users interacting with EM_BOOL signatures) Then #22155 changed Emscripten away from using The regression to this PR happened at some point in those refactors I think. |
|
Or actually, no, it looks like it is a i8 field still. I'll update to that form. |
| #endif | ||
| #if GL_SUPPORT_EXPLICIT_SWAP_CONTROL | ||
| {{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.explicitSwapControl, 'c.attributes.explicitSwapControl', 'i8') }}}; | ||
| {{{ makeSetValue('a', C_STRUCTS.EmscriptenWebGLContextAttributes.explicitSwapControl, 'c.attributes.explicitSwapControl', 'i32') }}}; |
There was a problem hiding this comment.
There is also an access in
src/lib/libwebgl.js: {{{ makeSetValue('handle', 0, 'webGLContextAttributes.explicitSwapControl', 'i8')}}};
which should change to i32.
| registerPreMainLoop(() => { | ||
| if (GL.currentContext && GL.currentContextIsProxied) { | ||
| var explicitSwapControl = {{{ makeGetValue('GL.currentContext', 0, 'i32') }}}; | ||
| var explicitSwapControl = {{{ makeGetValue('GL.currentContext', 0, 'i8') }}}; |
There was a problem hiding this comment.
I'm confused. Elsewhere in this file we do things like GL.currentContext.tempVertexBuffers1 which suggests GL.currentContext is an object. Is that not the case?
It looks like it is, as it is written to from GL.contexts entries, which are objects?
There was a problem hiding this comment.
That is indeed a bit confusing by static reading.
What happens is that when GL.currentContextIsProxied is true, then GL.currentContext is not an actual GL context handle (since the proxy target thread owns it), but a pointer to a control block of that context, so a number. Updated this site with a comment.
There was a problem hiding this comment.
Now I see, thanks. But I see this:
typedef struct WebGLContextHandle {
uint32_t explicit_swap_control;
pthread_t owning_thread;
} WebGLContextHandle;So shouldn't this be i32, not i8? From the PR description it sounds like the write is of i8, but if so, can we define the field in C to be an i8 too for consistency?
There was a problem hiding this comment.
Also, where is that write? I can't seem to find it by grepping.
| ctx.drawImage(canvas, 0, 0); | ||
| var pixel = ctx.getImageData(canvas.width >> 1, canvas.height >> 1, 1, 1).data; | ||
| out('canvas pixel: ' + Array.from(pixel)); | ||
| return pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 255 && pixel[3] == 255; |
There was a problem hiding this comment.
Should we just assert this line here in JS? Then if it crashes the debugger will show the correct location.
Also we can remove int presented completely.
Keep writing the bool explicitSwapControl attribute as i8; writing it as i32 at its unaligned offset would clobber the neighboring struct fields. In the test, assert the canvas pixel directly in JS and remove the static keywords. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ontexts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -sOFFSCREEN_FRAMEBUFFER, -pthread and
-sGL_SUPPORT_EXPLICIT_SWAP_CONTROL, a WebGL context that is proxied to the main thread and created with explicitSwapControl=false is supposed to be presented by the runtime at each main loop iteration. Three earlier changes each broke a part of this:
__postjssuffix. That is not a JS library decorator, so the hook was never emitted. The key was instead treated as a library symbol of its own, which showed up in builds that include the whole JS library (-sINCLUDE_FULL_LIBRARY, -sMAIN_MODULE=1). Use__postset.$registerPreMainLoopdependency: since Automate JS-to-JS library dependencies using Acorn #27995 it is detected automatically, and listing it is reported as redundant.No test passed -sGL_SUPPORT_EXPLICIT_SWAP_CONTROL, and the one test of a proxied implicit-swap context (test_emscripten_animate_canvas_element_size) only checks the exit code. Add a browser test that reads back the canvas to check that the frame was presented.